package com.aiju.cash.ui.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.aiju.app.config.ConfigManager;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.LoginApi;
import com.aiju.cash.ui.login.LoginActivity;
import com.aiju.common.ActivityStack;
import com.aiju.utils.L;
import com.aiju.utils.PreferencesUtils;
import com.aiju.utils.T;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2016-12-19 13:49
 * @version: V1.0
 * @Description: 个人设置
 */
public class UserActivity extends BaseTitleActivity {

    private TextView nameTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_setting);
        setCenterTitle("个人设置");

        //登录账号
        TextView idTextView = (TextView) findViewById(R.id.tv_user_id);
        idTextView.setText(mUser.getPhone());

        //昵称
        nameTextView = (TextView) findViewById(R.id.tv_user_name);


        //修改昵称
        findViewById(R.id.ly_user_name).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UserActivity.this, StoreNameActivity.class);
                intent.putExtra("showName", mUser.getUserName());
                intent.putExtra("showType", StoreNameActivity.SHOW_USER);
                startActivity(intent);
            }
        });

        //修改密码
        findViewById(R.id.ly_user_password).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(UserPasswordActivity.class);
            }
        });

        //退出登录
        findViewById(R.id.btn_login_out).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new LoginApi().logout(mUser.getToken(), ConfigManager.getInstance().getRegId(),new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        T.ToastNetError();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        DataJson dataJson = new DataJson(response);
                        if (dataJson.isSuccess()) {
                            T.ToastShort("注销成功");
                            //PreferencesUtils.putString(UserActivity.this, "name", "");
                            PreferencesUtils.putString(UserActivity.this, "password", "");
                            ActivityStack.finishAll();
                            startActivity(LoginActivity.class);
                        } else {
                            T.ToastShort("注销失败");
                            L.e(dataJson.getMsg());
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        nameTextView.setText(mUser.getUserName());
    }
}
